package cn.wxxyhw.server;

import cn.wxxyhw.Dao.LiuyanDao;
import cn.wxxyhw.Entity.Article;
import cn.wxxyhw.Util.DateFormateUtil;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @Author: 洪稳
 * @Date: 2019/6/4
 */
@Service
public class Liuyan {
    @Autowired
    private LiuyanDao liuyanDao;

    /**
     * 查询所有留言
     * @param uid 留言所有者
     * @return
     */
    public List<cn.wxxyhw.Entity.Liuyan> getList(String uid){
        List<cn.wxxyhw.Entity.Liuyan> allLiuyanByName = liuyanDao.getAllLiuyanByName(uid);
        if (allLiuyanByName!=null)
            return allLiuyanByName;
        return null;
    }

    /**
     * 保存留言
     * @param desc 留言信息
     * @param fromName  留言者
     * @param toName  被留言者
     * @param realPath  文件路径
     */
    public void saveLiuYan(String desc, String fromName, String toName, String realPath,String fromId,String toId) {
        if (realPath!=null)
            desc+="<div><img src='/image/"+realPath+"'  width='150px'/></div>";
        cn.wxxyhw.Entity.Liuyan liuyan = new cn.wxxyhw.Entity.Liuyan();
        liuyan.setDate(DateFormateUtil.getDateFormat().format(new Date()));
        liuyan.setFromName(fromName);
        liuyan.setToName(toName);
        liuyan.setToId(toId);
        liuyan.setFromId(fromId);
        liuyan.setContent(desc);
        liuyanDao.save(liuyan);

    }

    /**
     * 删除留言
     * @param id
     */
    public void del(String id) {
        liuyanDao.del(Integer.parseInt(id));
    }


}
